package com.example.myapplication.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemChildClickListener;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.example.myapplication.BaseFgm;
import com.example.myapplication.R;
import com.example.myapplication.activity.SellItemDetailAty;
import com.example.myapplication.adp.MarketListAdp;
import com.example.myapplication.database.SellItem;
import com.example.myapplication.util.ActivityStack;
import com.example.myapplication.util.ToastUtil;

import org.litepal.LitePal;

import java.util.ArrayList;
import java.util.List;

public class MarketListFgm extends BaseFgm {
    private ArrayList<SellItem> datas=new ArrayList<SellItem>();
    private RecyclerView listView;
    private MarketListAdp adp;
    private int type;
    /**
     *
     * @param type 1书籍 2-电子 3衣物  4日杂 5回收
     * @return
     */
    public static MarketListFgm getInstance(int type){
        MarketListFgm listFgm = new MarketListFgm();
        Bundle bundle = new Bundle();
        bundle.putInt("type",type);
        listFgm.setArguments(bundle);
        return listFgm;
    }

    @Override
    protected void logicStart() {
        datas.clear();

        listView.setLayoutManager(new GridLayoutManager(getContext(),2));
        listView.setAdapter(adp);
        adp.addHeaderView(LayoutInflater.from(getContext()).inflate(R.layout.dv_12dp,null));



        type=getArguments().getInt("type");
        String filter="";
        switch (type){
            case 1:
                filter="书籍";
                break;
            case 2:
                filter="电子";
                break;
            case 3:
                filter="衣物";
                break;
            case 4:
                filter="日杂";
                break;

        }
        try {
            List<SellItem> sellItems = LitePal.where("type = '" + filter + "'").find(SellItem.class);
            datas.addAll(sellItems);
            adp.notifyDataSetChanged();
        }catch (Exception e){
            e.printStackTrace();

        }


    }

    @Override
    protected void initView() {
        adp=new MarketListAdp(datas);

        type=getArguments().getInt("type");
        listView=rootView.findViewById(R.id.listView);
    }

    @Override
    public void refreshState() {
        super.refreshState();
        datas.clear();
        type=getArguments().getInt("type");
        String filter="";
        switch (type){
            case 1:
                filter="书籍";
                break;
            case 2:
                filter="电子";
                break;
            case 3:
                filter="衣物";
                break;
            case 4:
                filter="日杂";
                break;

        }
        try {
            List<SellItem> sellItems = LitePal.where("type = '" + filter + "'").find(SellItem.class);
            datas.addAll(sellItems);
            adp.notifyDataSetChanged();
        }catch (Exception e){
            e.printStackTrace();

        }
    }

    @Override
    protected void setListener() {
    }

    @Override
    protected int getlayout() {
        return R.layout.lo_list;
    }
}
